// pages/songDetail/songDetail.js
import request from '../../utils/request'
import pubsub from 'pubsub-js'
import moment from 'moment'

const appInstance = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isPlay: false,
        song: {},
        musicId: '',
        musicLink: '',
        currentTime: '00:00',
        durationTime: '00:00',
        currentWidth: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //创建控制音乐播放的实例
        this.backgroundAudioManager = wx.getBackgroundAudioManager();

        //options 接收路由跳转的query参数
        let musicId = options.musicId;
        this.setData({
            musicId
        });
        this.getMusicDetail(musicId);
        //判断当前音乐是否在播放
        if (appInstance.globalData.isMusicPlay && appInstance.globalData.musicId === musicId) {
            //修改当前音乐播放状态为true
            this.setData({
                isPlay: true
            });
        } else {
            this.changePlayState(false);
            this.backgroundAudioManager.pause();
        }

        this.backgroundAudioManager.onPlay(() => {
            this.changePlayState(true);
            //修改全局播放状态
            appInstance.globalData.musicId = musicId;
        });
        this.backgroundAudioManager.onPause(() => {
            this.changePlayState(false);
        });
        this.backgroundAudioManager.onStop(() => {
            this.changePlayState(false);
        });

        //监听音乐的进度
        this.backgroundAudioManager.onTimeUpdate(() => {
            let currentTime = moment(this.backgroundAudioManager.currentTime * 1000).format("mm:ss");

            //计算实时进度条的宽度
            let currentWidth = this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration * 450;
            // console.log(currentWidth,111);
            this.setData({
                currentTime,
                currentWidth
            });
        });
        //监听音乐播放自然结束
        this.backgroundAudioManager.onEnded(() => {
            //自动切换至下一首  自动播放
            pubsub.publish('switchType','next');
        });
    },

    //修改播放状态
    changePlayState(isPlay) {
        this.setData({
            isPlay
        });
        appInstance.globalData.isMusicPlay = isPlay;
    },

    async getMusicDetail(musicId) {
        let result = await request("/song/detail", {
            ids: musicId
        });

        let durationTime = moment(result.songs[0].dt).format('mm:ss')

        this.setData({
            song: result.songs[0],
            durationTime
        });
        wx.setNavigationBarTitle({
            title: this.data.song.name,
        });
    },

    handlerMusicPlay() {
        let isPlay = !this.data.isPlay;
        // this.setData({
        //     isPlay
        // });
        let {
            musicLink
        } = this.data;
        this.musicControl(isPlay, musicLink);
    },

    //控制音乐的播放
    async musicControl(isPlay, musicLink) {
        if (isPlay) {
            if (!musicLink) {
                //获取音乐播放链接
                let musicLinkData = await request("/song/url", {
                    id: this.data.musicId
                });
                musicLink = musicLinkData.data[0].url;
                this.setData({
                    musicLink
                });
            }

            this.backgroundAudioManager.src = musicLink;
            this.backgroundAudioManager.title = this.data.song.name; //必填
        } else {
            this.backgroundAudioManager.pause(); //暂停
        }
    },

    //控制音乐的切换
    handleSwitch(event) {
        let type = event.currentTarget.id;

        //先关闭当前播放的音乐
        this.backgroundAudioManager.stop();

        pubsub.subscribe("musicId", (msg, musicId) => {
            console.log(musicId);
            //获取音乐详情
            this.getMusicDetail(musicId);
            this.setData({
                musicId
            });
            //自动播放
            this.musicControl(true);

            pubsub.unsubscribe("musicId");
        });
        pubsub.publish('switchType', type);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})